package com.puntodonna.fogliocassa.client.app.presenter.main;

import com.google.gwt.event.shared.EventBus;
import com.google.gwt.event.shared.GwtEvent.Type;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.inject.Inject;
import com.gwtplatform.mvp.client.Presenter;
import com.gwtplatform.mvp.client.View;
import com.gwtplatform.mvp.client.annotations.ContentSlot;
import com.gwtplatform.mvp.client.annotations.ProxyCodeSplit;
import com.gwtplatform.mvp.client.proxy.AsyncCallFailEvent;
import com.gwtplatform.mvp.client.proxy.AsyncCallFailHandler;
import com.gwtplatform.mvp.client.proxy.AsyncCallStartEvent;
import com.gwtplatform.mvp.client.proxy.AsyncCallStartHandler;
import com.gwtplatform.mvp.client.proxy.AsyncCallSucceedEvent;
import com.gwtplatform.mvp.client.proxy.AsyncCallSucceedHandler;
import com.gwtplatform.mvp.client.proxy.Proxy;
import com.gwtplatform.mvp.client.proxy.RevealContentEvent;
import com.gwtplatform.mvp.client.proxy.RevealContentHandler;
import com.gwtplatform.mvp.client.proxy.RevealRootLayoutContentEvent;
import com.puntodonna.fogliocassa.client.app.presenter.header.HeaderPresenter;
import com.puntodonna.fogliocassa.client.resources.FoglioCassaResources;

public class MainPresenter extends Presenter<MainPresenter.MyView, MainPresenter.MyProxy> implements AsyncCallStartHandler, AsyncCallSucceedHandler, AsyncCallFailHandler {

	@ContentSlot
	public static final Type<RevealContentHandler<?>> TYPE_WIDGET_AREA = new Type<RevealContentHandler<?>>();
	@ContentSlot
	public static final Type<RevealContentHandler<?>> TYPE_HEADER_AREA = new Type<RevealContentHandler<?>>();

	public interface MyView extends View {
	}

	@ProxyCodeSplit
	public interface MyProxy extends Proxy<MainPresenter> {
	}

	@Inject
	public MainPresenter(final EventBus eventBus, final MyView view, final MyProxy proxy) {
		super(eventBus, view, proxy);
	}
	
	@Inject
	protected FoglioCassaResources resources;
	
	@Inject
	protected HeaderPresenter headerPresenter;

	protected DialogBox dialogBox;

	@Override
	protected void revealInParent() {
		RevealRootLayoutContentEvent.fire(this, this);
		RevealContentEvent.fire(this, MainPresenter.TYPE_HEADER_AREA, this.headerPresenter);
	}

	@Override
	protected void onBind() {
		super.onBind();
		addRegisteredHandler(AsyncCallStartEvent.getType(), this);
		addRegisteredHandler(AsyncCallFailEvent.getType(), this);
		addRegisteredHandler(AsyncCallSucceedEvent.getType(), this);
	}
	
	@Override
	protected void onReset() {
		super.onReset();
	}
	
	protected DialogBox createLoadingDialogBox() {
		DialogBox dialogBox = new DialogBox();
		dialogBox.setAnimationEnabled(true);
		dialogBox.setGlassEnabled(true);
		dialogBox.setAutoHideEnabled(false);
		dialogBox.setText("loading...");
		HorizontalPanel horizontalPanel = new HorizontalPanel();
		horizontalPanel.setSize("100%", "100%");
		horizontalPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		horizontalPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		horizontalPanel.add(new Image(getResources().loadingImg()));
		dialogBox.setWidget(horizontalPanel);
		return dialogBox;
	}

	public FoglioCassaResources getResources() {
		return resources;
	}

	public DialogBox getDialogBox() {
		if (this.dialogBox == null)
			this.dialogBox = createLoadingDialogBox();// loading popup panel
		return dialogBox;
	}
	
	@Override
	public void onAsyncCallFail(AsyncCallFailEvent asyncCallFailEvent) {
		getDialogBox().hide();
	}

	@Override
	public void onAsyncCallSucceed(AsyncCallSucceedEvent asyncCallSucceedEvent) {
		getDialogBox().hide();
	}

	@Override
	public void onAsyncCallStart(AsyncCallStartEvent asyncCallStartEvent) {
		getDialogBox().center();
	}

}
